import pandas as pd

def capitalize_only_first_letter(s):
    if not s:  # Check if the string is empty
        return s
    return s[0].upper() + s[1:]


if __name__ == '__main__':
    # Load the CSV file
    file_path = 'D:\\pros\\vs\\ChipAssistant\\Document\\TMI85303寄存器汇总.csv'
    csv_data = pd.read_csv(file_path)

    # Define a template for the C# properties
    cs_template = """
    public partial class RegVM : ObservableValidator
    {{
        private RegVM()
        {{
            WeakReferenceMessenger.Default.Register<RefreshMTP, int>(this, (int)MesgToken.RegVMToken, OnRefreshMTP);
        }}
        
        {properties}
    }}
    """

    # Template for each property block, including default, max, and min values
    slider_property_template = ("\n"
                                "    	// {name} property\n"
                                "    	private int {name} = {default_value};\n"
                                "        [Required]\n"
                                "        [Range({min_value},{max_value})]\n"
                                "    	public int {attri_name}\n"
                                "    	{{\n"
                                "    	    get => {name};\n"
                                "    	    set\n"
                                "    	    {{\n"
                                "    	        if(TrySetProperty(ref {name}, value, out _))\n"
                                "                {{\n"
                                "                    \n"
                                "                }}\n"
                                "    	    }}\n"
                                "    	}}\n"
                                "    	[ObservableProperty]\n"
                                "    	private {show_type} {name}Show = {show_default};\n"
                                "    	public int {attri_name}Max {{ get; set; }} = {max_value};\n"
                                "    	public int {attri_name}Min {{ get; set; }} = {min_value};\n"
                                "    ")

    radio_property_template = """
    	// {name} property
    	private int {name} = {default_value};
        [Required]
        [Range({min_value},{max_value})]
    	public int {attri_name} 
    	{{
    	    get => {name};
    	    set => TrySetProperty(ref {name}, value, out _);
    	}}
    	public int {attri_name}Max {{ get; set; }} = {max_value};
    	public int {attri_name}Min {{ get; set; }} = {min_value};
    """

    # Generate the properties string
    properties_str = "#region 寄存器参数"
    for _, row in csv_data.iterrows():
        name = row['寄存器名字（String）']
        attri_name = capitalize_only_first_letter(row['寄存器名字（String）'])
        default_value = row['默认初始值']
        max_value = row['最大值']
        min_value = row['最小值']
        show_type = row['ShowType']
        show_default = row['ShowDefault']

        control = row['所属控件（1：Slider；2：单选按钮）']
        if (control == 1):
            properties_str += slider_property_template.format(name=name, attri_name=attri_name,
                                                              default_value=default_value, max_value=max_value,
                                                              min_value=min_value,show_type=show_type,show_default=show_default)
        else:
            properties_str += radio_property_template.format(name=name, attri_name=attri_name, default_value=default_value, max_value=max_value,
                                                          min_value=min_value)

    properties_str += "#endregion\n\n"

    sliderOut = "   #region Slider拖动触发事件"

    RadioOut = "    #region RadioButton触发事件"

    property_SlierFun = """
    	// {name} property 滑块触动事件
    	[RelayCommand]
        private void {attri_name}DragCompleted()
        {{
        
        }}
    """

    property_RadionFun = """
    	// {name} property 触动事件
    	[RelayCommand]
        private void {attri_name}Click()
        {{

        }}
    """


    for _, row in csv_data.iterrows():
        name = row['寄存器名字（String）']
        attri_name = capitalize_only_first_letter(row['寄存器名字（String）'])
        control = row['所属控件（1：Slider；2：单选按钮）']
        if(control == 1):
            sliderOut += property_SlierFun.format(name=name,attri_name=attri_name)
        else:
            RadioOut += property_RadionFun.format(name=name,attri_name=attri_name)

    sliderOut += "#endregion\n\n"
    RadioOut += "#endregion\n\n"

    properties_str += sliderOut
    properties_str += RadioOut

    # Combine the template with the generated properties string
    cs_code = cs_template.format(properties=properties_str.strip())

    # Output the C# code to a .cs file
    cs_file_path = 'D:\\pros\\vs\\ChipAssistant\\Document\\TMI85303寄存器代码输出.cs'
    with open(cs_file_path, 'w') as cs_file:
        cs_file.write(cs_code)